package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.Util.PageResult;
import com.story.common.persistence.model.CategoryClass;
import com.story.common.persistence.model.pageclass;
import com.story.core.base.controller.BaseController;
import com.story.modular.app.service.IPageclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.Map;

/**
 * 首页分类控制器
 *
 * @author fengshuonan
 * @Date 2020-05-12 17:26:07
 */
@Controller
@RequestMapping("/pageclass")
public class PageclassController extends BaseController {

    private String PREFIX = "/app/pageclass/";

    @Autowired
    private IPageclassService pageclassService;

    /**
     * 跳转到首页分类首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "pageclass.html";
    }

    /**
     * 跳转到添加首页分类
     */
    @RequestMapping("/pageclass_add")
    public String pageclassAdd() {
        return PREFIX + "pageclass_add.html";
    }

    /**
     * 跳转到修改首页分类
     */
    @RequestMapping("/pageclass_update/{pageclassId}")
    public String pageclassUpdate(@PathVariable Integer pageclassId, Model model) {
        pageclass categoryClass=pageclassService.findById(pageclassId);
        model.addAttribute("pageclass",categoryClass);
        return PREFIX + "pageclass_edit.html";
    }

    /**
     * 获取首页分类列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name,@RequestParam(required=false,defaultValue="1") int page,
                       @RequestParam(required=false,defaultValue="10") int limit) {
        Map<String,Object> map=new HashMap<String,Object>();
        if (!"".equals(name) && name !=null){
            map.put("name",name);
        }
        Page<pageclass> page1 = new Page<pageclass>();

        //page.setRecords(categoryService.queryPageList(page,condition));

        page1.setCurrent(page);
        page1.setSize(limit);
        PageResult pageResult=new PageResult();
        pageResult.setData(pageclassService.queryPageList(page1,name));
        pageResult.setCode("0");
        int total=page1.getTotal();
        System.out.println(total);
        pageResult.setCount(page1.getTotal());

        pageResult.setMsg("首页顶部分类");
        return pageResult;
    }
    /**
     * 获取首页分类列表 不带分页
     */
    @RequestMapping(value = "/queryList")
    @ResponseBody
    public Object querylist(pageclass pageclass) {
        return pageclassService.quyerList(pageclass);
    }
    /**
     * 新增首页分类
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(pageclass pageclass) {
        pageclassService.save(pageclass);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除首页分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "ids") String ids) {
        String[] idArray=ids.split(",");
        for (int i=0;i<idArray.length;i++){
            pageclassService.delete(pageclassService.findById(Integer.parseInt(idArray[i])));
        }
        return SUCCESS_TIP;
    }


    /**
     * 修改首页分类
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(pageclass pageclass) {
        if(pageclassService.update(pageclass)>0){
            return super.SUCCESS_TIP;
        }else {
            return super.ERROR;
        }
    }

    /**
     * 首页分类详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
